-- evil_absorb
-- create by zhuangsl
-- 邪灵吸能术

return {
    apply = function(source, target, skillId, round, para, extra)
        local combine = PropM.combine;
        local PropM_apply = PropM.apply;
        local fetchProps = PropM.fetchProps;
        local trigger = PropM.trigger;

        -- 邪灵吸能术等级
        local prop = combine(source, "evil_absorb", 1);
        local level = math.max(prop[3], 1);

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);

        --最终威力
        local forceDamage = math.modf(para[1] * level * (addon_rate + 1000) / 1000);
        local forceCure = math.modf(para[2] * level * (addon_rate + 1000) / 1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 被对方百分比削弱
        prop = combine(target, "weak_enemy", "magic");
        magic = PropM_apply(prop, magic);

        -- 被对方绝对值削弱
        prop = combine(target, "weak_enemy2", "magic");
        magic = PropM_apply(prop, magic);

        -- 3. 伤害/计算伤害时魔力+3
        local damage = math.modf(forceDamage * (magic + 3) / 1000);
        local hp = math.modf(forceCure * (magic + 3) / 1000);
        local mp = math.modf(para[3] * level * (addon_rate + 1000) / 1000);

        -- 魔法暴击类，多种暴击线性叠加伤害
        addon_rate = 1000;

        -- 伤害类魔法概率增强
        local props = fetchProps(source, "damage_magic");
        for _, prop in ipairs(props) do
            addon_rate = addon_rate + trigger(source, prop[1], prop[2], 1000);
        end
        -- 魔法暴击
        props = fetchProps(source, "critical_magic");
        for _, prop in ipairs(props) do
            addon_rate = addon_rate + trigger(source, prop[1], prop[2], 1000);
        end
        -- 概率双倍
        props = fetchProps(source, "skill_double");
        for _, prop in ipairs(props) do
            if tonumber(prop[2]) == skillId then
                addon_rate = addon_rate + trigger(source, prop[1], prop[2], 1000);
            end
        end

        -- 最终效果
        damage = math.modf(damage * addon_rate / 1000);
        hp = math.modf(hp * addon_rate / 1000);

        -- 4. 魔法抗性
        prop = combine(target, "resist", "magic");
        -- 忽视抗性
        local p = combine(source, "ignore_resist", skillId);
        prop[3] = PropM_apply(p, prop[3]);
        p = combine(source, "ignore_resist", "magic");
        prop[3] = PropM_apply(p, prop[3]);

        damage = PropM_apply(prop, damage);
        hp = PropM_apply(prop, hp);
        mp = PropM_apply(prop, mp);

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 伤害，回复生命、魔法，提升攻击力
        CombatM.receiveDamage(source, target, damage, skillId, extra);
        CombatM.receiveCure(source, source, hp);
        BonusM.doBonus({ 2, "mp", mp, }, "cure_mp");
        BonusM.doBonus({ 2, "attack", para[4], }, "cure_mp");

        -- 事件
        EventMgr.fire(event.HP_VAMPIRE, hp);
        EventMgr.fire(event.MP_VAMPIRE, mp);

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 邪灵吸能术等级
        local prop = PropM.combine(source, "evil_absorb", 1);
        local level = math.max(prop[3], 1);

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 3. 伤害描述
        local base = para[1] * level;
        local force = math.modf(para[1] * level * (addon_rate + 1000) / 1000);

        local damageBase = math.modf(base * (magic + 3) / 1000);
        local damageForce = math.modf(force * (magic + 3) / 1000);
        local damageAddon = damageForce - damageBase;
        if damageAddon == 0 then
            desc = string.gsub(desc, "{damage}", damageForce);
        else
            desc = string.gsub(desc, "{damage}", damageBase .. "(+" .. damageAddon .. ")");
        end

        -- 4. 回复描述
        force = math.modf(para[2] * level * (addon_rate + 1000) / 1000);
        local hp = math.modf(force * (magic + 3) / 1000);
        desc = string.gsub(desc, "{cure_hp}", hp);

        local mp = math.modf(para[3] * level * (addon_rate + 1000) / 1000);
        desc = string.gsub(desc, "{cure_mp}", mp);

        return desc;
    end,
};
